package com.nation.controller;

import com.nation.domain.TVideo;
import com.nation.domain.User;
import com.nation.service.NationInfoService;
import com.nation.service.VideoService;
import com.nation.utils.ReadAndStoreFile;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by 凌 on 2017/11/21.
 */
@Controller
@RequestMapping(value = "/UploadAllFiles")
public class VideoController {
    @Autowired
    VideoService VideoService;
    @Autowired
    NationInfoService NationInfoService;

    @RequestMapping(value = "/upload_nation_files")
    public String upload_nation_files(){
        return "UploadAllFiles/upload_nation_files";
    }
    @RequestMapping(value = "/upload_literature_data_files")
    public String upload_literature_data_files(){
        return "UploadAllFiles/upload_literature_data_files";
    }
    @RequestMapping(value = "/upload_research_work_files")
    public String upload_research_work_files(){
        return "UploadAllFiles/upload_research_work_files";
    }
    @RequestMapping(value = "/upload_world_nation_files")
    public String upload_picture_files(){
        return "UploadAllFiles/upload_world_nation_files";
    }
    /*
   * 上传视频文件
   * @param*/
    @RequestMapping(value = "/uploadNationFile_videos.do")
    @ResponseBody
    public String uploadNationFile_videos(HttpServletRequest request,@RequestParam(value = "nation_files") MultipartFile[] nation_files, @RequestParam(value = "nation_z")String nation_z,
                                          @RequestParam(value = "title_video")String title_video){
        org.apache.shiro.subject.Subject currentuser= SecurityUtils.getSubject();
        User user=(User)currentuser.getPrincipal();
        String input_code=user.getYHZH();

        try{
            String path=request.getSession().getServletContext().getResource("/").getPath();
            path += ReadAndStoreFile.NATIONVIDEOS;
            System.out.println("path--"+path);

            String country_code=NationInfoService.getCountryCodeByNation_z(nation_z);
            System.out.println("country_code---"+country_code);
            String picture_path=null;
//          将MultipartFile格式转换成File
            for (int i=0;i<nation_files.length;i++){
                picture_path+=ReadAndStoreFile.save(nation_files[i],path);

                String video_code="V"+country_code;
                int video_num=VideoService.getNumOfVideo();
                String video_nums="";
                if (video_num<9){
                    video_num+=1;
                    video_nums+="0"+video_num;
                }
                video_code+=video_nums;
                TVideo tVideo=new TVideo(country_code,title_video,picture_path,video_code);
                VideoService.uploadNationFile_videos(tVideo);
            }
        }catch (IOException e){
            e.printStackTrace();
            return "上传失败";
        }
        return "上传成功";
    }

}
